﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using AspNetCs.Model;
using AspNetCs.IDAL;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class LoveWall_DAL:ILoveWall
    {

        /// <summary>
        /// 添加许愿信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int  AddLoveWallMsg(LoveWall_Model model)
        {
            int count = 0;
            db dbclass = new db();
            string cmdText = "insert into LoveWallInfo values (@PDate,@Content,@Author,@BgColor,@BgImg)";
            db.ParameterArry[] paras = new db.ParameterArry[5];

            paras[0].dbType = SqlDbType.DateTime;
            paras[0].parName = "@PDate";
            paras[0].objValue = model.PDate;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@Content";
            paras[1].objValue = model.Content;

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].parName = "@Author";
            paras[2].objValue = model.Author;

            paras[3].dbType = SqlDbType.Int;
            paras[3].parName = "@BgColor";
            paras[3].objValue = model.BgColor;

            paras[4].dbType = SqlDbType.Int;
            paras[4].parName = "@BgImg";
            paras[4].objValue = model.BgImg;
            
            
            count=dbclass.ExcSql(cmdText,paras);

            return count;
        }

        /// <summary>
        /// 获得留言墙的纸条数
        /// </summary>
        /// <returns></returns>
        public int GetLoveWallMsgNum()
        {
            int count = 0;
            string cmdText = "select count(id) from LoveWallInfo";
            db dbclass = new db();
            SqlDataReader reader = dbclass.GetSqlDataReader(cmdText);
            if(reader.Read())
            {
                count = reader.GetInt32(0);
            }
            dbclass.close();
            dbclass.Dispose();
            return count;
        }


        /// <summary>
        /// 获得留言纸条信息
        /// </summary>
        /// <param name="searchContent">查询条件</param>
        /// <param name="pageIndex">页的索引</param>
        /// <param name="pageSize">每页的留言纸条数</param>
        /// <param name="isCount">是否返回总条数</param>
        /// <returns></returns>
        public SqlDataReader getGetLoveWallMsg(string searchContent, int pageIndex,int pageSize,int isCount)
        {
            SqlDataReader reader = null;
            string cmdText = "exec GetRecordFromPage @tblName='LoveWallInfo',@RetColumns='*',@Orderfld='Id',@PageSize={0},@PageIndex={1},@IsCount={3},@OrderType='desc',@strWhere=' content like ''%{2}%''',@ident='Id'";
            cmdText = string.Format(cmdText, pageSize, pageIndex, searchContent, isCount);
            db dbclass = new db();
            reader = dbclass.GetSqlDataReader(cmdText);
            return reader;
        }
    }
}
